package com.infore.airsupervise.biz.impl;

import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.app.MyApplication;
import com.infore.airsupervise.bean.AQIRealDataE;
import com.infore.airsupervise.biz.AQIDataRankBiz;
import com.infore.airsupervise.http.BaseResponse;
import com.infore.airsupervise.http.HttpAPI;
import com.infore.airsupervise.http.HttpCallBack;
import com.infore.airsupervise.http.HttpUtil;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JSONUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * @features:  因子排名数据 网络请求接口 业务类
 * @author: WuTao
 * @date: 2017/3/24 17:17
 */
public class AQIDataRankBizImpl implements AQIDataRankBiz {

    /**
     * 实时排名
     * @param factorType
     * @param listener
     * @return
     */
    @Override
    public Call<BaseResponse> sendRealDataRankRequest(int factorType, final OnServerResponseListener<List<AQIRealDataE.RankBean>> listener) {
        Map<String,Object> params= new HashMap<>();
        params.put("factorType",factorType);
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.QUERYREALDATARANK,params);
        call.enqueue( new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<AQIRealDataE.RankBean> list = JSONUtil.parseList(response.getData(), Constants.RESPONSE_BEAN_LIST_KEY, AQIRealDataE.RankBean.class);
                listener.onSucceed(list,response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode,MsgConst.GET_DATA_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, title);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return call;
    }


    /**
     * 历史排名
     * @param time   yyyy-MM-DD HH:mm:ss
     * @param historyType
     * @param factorType
     * @param listener
     * @return
     */
    @Override
    public Call<BaseResponse> sendHistoryDataRankRequest(String time, int historyType, int factorType, final OnServerResponseListener<List<AQIRealDataE.RankBean>> listener) {
        Map<String,Object> params= new HashMap<>();
        params.put("factorType",factorType);
        params.put("time",time);
        params.put("historytype",historyType);

        final Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.QUERYHISTORYDATARANK,params);
        call.enqueue( new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<AQIRealDataE.RankBean> list = JSONUtil.parseList(response.getData(), Constants.RESPONSE_BEAN_LIST_KEY, AQIRealDataE.RankBean.class);
                listener.onSucceed(list,response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, title);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return call;
    }
}
